package com.csdj.order.service;

import com.csdj.beans.entity.Goods;
import com.csdj.beans.entity.Order;
import com.csdj.beans.entity.OrderExample;
import com.csdj.beans.interfaces.order.IOrderService;
import com.csdj.common.OrderException;
import com.csdj.common.constant.OrderConstant;
import com.csdj.order.mapper.OrderMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@DubboService
public class OrderService implements IOrderService {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private GoodsService goodsService;

    private Logger log = LoggerFactory.getLogger("orderService");

    /**
     * 保存订单到数据库
     * @param order
     * @return
     */
    @Override
    public Order saveOrder(Order order) {
        order.setCreateTm(new Date());
        order.setStatus(OrderConstant.ORDER_INIT);
        if(orderMapper.insert(order)>0)return order;
        return null;
    }

    @Override
    public List<Order> getOrderListByUserId(int userId) {
        OrderExample example = new OrderExample();
        example.createCriteria().andUserIdEqualTo(userId);
        example.setOrderByClause("id desc");
        return orderMapper.selectByExample(example);
    }

    /**
     * 只允许更新订单的状态，而且状㹜只能向后更新
     * @param order
     * @return
     */
    @Override
    public boolean update(Order order) {
        return orderMapper.updateStatus(order.getId(),order.getStatus())>0;
    }

    @Override
    public Order getById(int orderId) {
        return orderMapper.selectByPrimaryKey(orderId);
    }


    /**
     * 生成订单
     * @param uid
     * @param prodId
     * @param quantity
     * @return
     */
    @Transactional(rollbackFor = OrderException.class)
    @Override
    public Order generate(int uid, int prodId, int quantity,int points) {
        if(quantity <= 0)return null;
        Goods goods = goodsService.getById(prodId);
        if(goods == null){
            log.info("goods id:{} 商品不存在",prodId);
            return null;
        }

        if(goods.getStoreage()<quantity){
            log.info("goods id:{} 库存不足",prodId);
            return null;
        }

        if(goodsService.updateStorage(prodId,quantity)){
            //扣减库存成功
            Order order = new Order();
            order.setUserId(uid);
            order.setPrice(goods.getPrice());
            order.setDiscount(goods.getDiscount());
            order.setProdId(prodId);
            order.setQuantity((short)quantity);
            order.setCreateTm(new Date());
            order.setPoints(points);
            order.setStatus(OrderConstant.ORDER_INIT);
            if(orderMapper.insert(order)>0){
                return order;
            }else{
                throw new OrderException("保存订单失败");
            }
        }
        return null;
    }
}
